package test;

import java.util.Arrays;
import java.util.Scanner;

public class Question4 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt(), m = scanner.nextInt();

        int total = n*m;
        long[] height = new long[total];
        for (int i = 0; i < total; i++) {
            height[i] = scanner.nextLong();
        }
        Arrays.sort(height);


        long[] rowMax = new long[n];
        Arrays.fill(rowMax, -1L);

        int hugCount = 0;
        for (int i = 0; i < total; i++) {
            int row = i /m;
            long h = height[i];
            if(rowMax[row] >= h){
                hugCount++;
            }
            if(h > rowMax[row]){
                rowMax[row] = h;
            }
        }
        System.out.println(hugCount);
    }
}
